package com.zjj.lbw.ai;

import com.google.common.collect.Lists;
import dev.langchain4j.agent.tool.Tool;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatLanguageModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.zhipu.ZhipuAiChatModel;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.service.SystemMessage;

import java.time.LocalDateTime;
import java.util.List;

/**
 * AI Agent是指人工智能代理（Artificial Intelligence Agent）是一种能够感知环境进行自主理解，
 * 进行决策和执行动作的智能体。AI Agent具备通过独立思考、调用工具逐步完成给定目标的能力
 */
public class AIServiceToolsTest {
    static class User {
        private String username;
        private Integer age;

        public User(String username, Integer age) {
            this.username = username;
            this.age = age;
        }
    }

    static class MyTools {
        @Tool("用来获取当前具体日期")
        public static String dateUtil(String onUse) {
            return LocalDateTime.now().toString();
        }

        @Tool("获取指定日期注册的用户信息")
        public static List<User> getUserInfo(String date) {
            System.out.println("接收到的date参数的值：" + date);
            User user1 = new User("子坚君", 18);
            User user2 = new User("曹操", 18);
            return Lists.newArrayList(user1, user2);
        }
    }

    interface UserService {

        @SystemMessage("先获取具体日期，然后再解决用户问题")
        String getUserInfo(String desc);
    }

    public static void main(String[] args) {

//        ChatLanguageModel model = ZhipuAiChatModel
//                .builder()
//                .apiKey("9a6dfc4258104e28ad0c72db2ddda3e4.f6VL6AWRQxORuJpi")
//                .build();
        ChatLanguageModel model = OpenAiChatModel.builder()
                .baseUrl("https://hk.xty.app/v1")
                .apiKey("sk-8fcARj3z6BqDaPqlC0pB9pbeCVwPVqhVMVIzjrJ92WyisbTV")
                .modelName("gpt-4o")
                .build();

        UserService userService = AiServices.builder(UserService.class).chatLanguageModel(model)
                .tools(new MyTools())
                .chatMemory(MessageWindowChatMemory.withMaxMessages(10))
                .build();

        String userInfo = userService.getUserInfo("获取今天的注册的新用户信息");
        System.out.println(userInfo);

    }
}
